package com.mate.cloud.sensitive.processor;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = AESProcessorTest.class)
public class AESProcessorTest {

    private AESProcessor aesProcessorUnderTest;

    @BeforeEach
    void setUp() {
        aesProcessorUnderTest = new AESProcessor("secret");
    }

    @Test
    void testDecrypt() {
        // Setup
        // Run the test
        final byte[] result = aesProcessorUnderTest.decrypt("text");

        // Verify the results
        assertThat(result).isEqualTo("content".getBytes());
    }

    @Test
    void testEncrypt() {
        // Setup
        // Run the test
        final byte[] result = aesProcessorUnderTest.encrypt("content".getBytes());

        // Verify the results
        assertThat(result).isEqualTo("content".getBytes());
    }


    @Test
    public void keyTest() {
        //随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        String encode = Base64Encoder.encode(key);
        System.out.println("encode:" + encode);
        AESProcessor processor = new AESProcessor(encode);
        byte[] encrypt = processor.encrypt("测试数据加密".getBytes());
        String encode1 = Base64Encoder.encode(encrypt);
        System.out.println("encode1:" + encode1);
        byte[] decrypt = processor.decrypt(encode1);
        System.out.println("decrypt:" + StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
    }
}
